<?php
namespace app\Traits;

trait DataList{
      /**
     * 模糊查询
     */
    public function M_fuzzy()
    {
        if (!$this->fuzzyConditions) {
            return;
        }
        $fuzzys = $this->fuzzyConditions;
        foreach ($fuzzys as $k => $v) {
            if (array_key_exists($v, $this->param)) {
                $this->where('' . $this->name . '.' . $v, 'like', '%' . $this->param[$v] . '%');
            }

        }
    }

    /**
     * 精确查询
     */
    public function M_clear()
    {
        if (!$this->clearConditions) {
            return;
        }
        $clears = $this->clearConditions;
        foreach ($clears as $k => $v) {
            if ( array_key_exists($v, $this->param)) {
                $this->where('' . $this->name . '.' . $v, '=', $this->param[$v]);
            }
        }
    }

    /**
     * 范围查询
     */
    public function M_range()
    {
        if (!$this->rangeConditions) {
            return;
        }
        $ranges = $this->rangeConditions;
        foreach ($ranges as $k => $v) {
            if (array_key_exists($v, $this->param)) {
                $temp_arr = explode('--', $this->param[$v]);
                $temp_min = $temp_arr[0];
                $temp_max = $temp_arr[1];
                unset($temp_arr);
                $this->where('' . $this->name . '.' . $v, '>=', $temp_min)->where('' . $this->name . '.' . $v, '<=', $temp_max);
            }
        }
    }

    /**
     * 多表多条件排序
     */
    public function M_order()
    {
        if (!$this->orderByString) {
            return;
        }
        $orderBy = $this->orderByString;
        $orderArr = explode('.', $orderBy);
        if (!isset($this->union_fuzzyConditions)) {
            if ($orderArr[1] == 'descending') {
                $this->order('' . $this->name . '.' . $orderArr[0] . ' DESC');
            } else {
                $this->order('' . $this->name . '.' . $orderArr[0]);
            }
        } else {
            $union = $this->union_fuzzyConditions;
            foreach ($union as $k => $v) {
                if ($v[1] == $orderArr[0]) {
                    if ($orderArr[1] == 'descending') {
                        $this->order($v[0] . '.' . $orderArr[0] . ' DESC');
                    } else {
                        $this->order($v[0] . '.' . $orderArr[0]);
                    }
                } else {
                    if ($orderArr[1] == 'descending') {
                        $this->order('' . $this->name . '.' . $orderArr[0] . ' DESC');
                    } else {
                        $this->order('' . $this->name . '.' . $orderArr[0]);
                    }
                }
            }
        }
    }

    /**
     * 自定义全局特殊搜索条件
     */
    public function specialCondition()
    {
        $name = $this->specialConditions[0];
        $condition = $this->specialConditions[1];
        $value = $this->specialConditions[2];
        $this->where($name, $condition, $value);
    }

    /**
     * 联合查询
     */
    public function M_UnionSelect()
    {
        if (!$this->foreignConditions) {
            return;
        }
        $foreignConditions = $this->foreignConditions;
        foreach ($foreignConditions as $k => $v) {
            if ($v[2]) {
                $this->join($k, $k . '.' . $v[0] . '=' . $v[2] . '.' . $v[1], 'LEFT');
            } else {
                $arr = explode('.', $v[1]);
                if (count($arr) == 2) {
                    $this->join($k, $k . '.' . $v[0] . '=' . $v[1], 'LEFT');
                } else if (count($arr) == 1) {
                    $this->join($k, $k . '.' . $v[0] . '=' . '' . $this->name . '.' . $v[1], 'LEFT');
                }
            }
        }
    }

    /**
     * 对字段的联合查询
     */
    public function M_union_field()
    {
        $this->M_union_fuzzy();
    }

    /**
     * 字段的联合模糊查询
     */
    public function M_union_fuzzy()
    {
        if (!$this->union_fuzzyConditions) {
            return;
        }
        $union_fuzzys = $this->union_fuzzyConditions;
        foreach ($union_fuzzys as $k => $v) {
            if ($this->param[$v[1]]) {
                $this->where($v[0] . '.' . $v[1], 'like', '%' . $this->param[$v[1]] . '%');
            }

        }
    }

    /**
     * 自定义特殊字段
     */
    public function SetspecialCondition($name, $condition, $value)
    {
        $this->specialConditions = [$name, $condition, $value];
    }

    /**
     * 设置请求参数
     */
    public function setParam($param)
    {
        $this->param = $param;
    }
}